<template>
  <view class="big">
    <!-- <view class="emptyBox"></view>
    <view class="title">
      <Left class="backIcon" @click="goback"></Left>
      <view class="titleMsg">取货</view>
    </view> -->
    <headerSearch :query="query" @queryList="queryList" @search="search"></headerSearch>
    <scroll-view :scroll-y="true" class="search_list" @scrolltolower="handleScroll" v-if="showList.length">
      <template v-for="(item, index) in showList">
        <pick-up-list :item="item" :index="index" @chooseAll="chooseAll" @gotoForwarding="gotoForwarding" @gotoCallSome="gotoCallSome" @pickUpOne="pickUpOne" />
      </template>
      <view class="divide"></view>
    </scroll-view>
    <view v-else>
      <div class="emptyMain">
        <image class="emptyImg" src="../../assets/images/index/我的订单/wodedingdan_img_quesheng.png" mode="heightFix"></image>
        <p>暂无订单</p>
      </div>

    </view>
    <view class="pickUpfoot">
      <nut-checkbox v-model="checkAll" @change="checkBtn" label="全选">全选</nut-checkbox>
      <view class="pickUpBtnBox">
        <view class="zhaorendaiqu" @click="toCallSome">
          <image src="../../assets/images/pickUp/zhaorendaiqu.png"></image>
          <view class="img_text">找人代取</view>
        </view>
        <view class="zhuandaisong" @click="forwardingMore" >
          <image src="../../assets/images/pickUp/zhuandaisong.png"></image>
          <text class="img_text">转代送</text>
        </view>
        <nut-button type="info" @click="pickMore" color="#0966ff">办理取货</nut-button>
      </view>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, watch } from "vue";
import { changeList, $post, $get } from "@/@api";
import pickUpList from "./pickUpList.vue";
import headerSearch from "./headerSearch.vue";
import { useDidShow } from "@tarojs/taro";
import Taro from "@tarojs/taro";
import useUserInfoStore from "@/store/user";
import usePickUpBatchStore from "@/store/pickUpBatch";
import useTransactionStore, { transaction } from "@/store/pickUp";
import forwardInfoStore from "@/store/forward";
const userStore = useUserInfoStore();
const transactionStore = useTransactionStore();
const pickUpBatchStore = usePickUpBatchStore();
const forwardStore = forwardInfoStore();
const checkAll = ref<boolean>(false); //全选按钮
const checkList = ref(); //选中的单子集合


const query = ref({
  keywords: "",
  startBillingTime: "",
  endBillingTime: "",
});

const loading = ref(false);
const showList = ref<any>([]);

// 懒加载参数
const list = ref<any>([]);

function handleScroll() {
}
function chooseAll(flag: boolean) {
  if (flag) {
    checkAll.value = true;
  } else {
    checkAll.value = false;
  }
}
function checkBtn() {
  if (!checkAll.value) {
    showList.value.forEach((aa: any) => {
      aa.check = false
      aa.orderList.forEach((bb:any) => {
        bb.check = false
      })
    });
  } else {
    showList.value.forEach((aa: any) => {
      aa.check = true;
      aa.orderList.forEach((bb:any) => {
        bb.check = true
      })
    });
  }
}


function queryList(item) {
  if(item.start){
    query.value.startBillingTime = item.start + ' 00:00:00'
  }
  else {
    query.value.startBillingTime = ''
  }
  if(item.end){
    query.value.endBillingTime = item.end + ' 23:59:59'
  }
  else {
    query.value.endBillingTime = ''
  }
  showList.value = []
  initList();
}
//搜索
function search(keywords: string) {
  showList.value = []
  initList()
}
//单项找人代取
const gotoCallSome = (obj)=>{
  let data = [
    showList.value[obj.index]
  ]
  data[0].orderList = [showList.value[obj.index].orderList[obj.orderIndex]]
  pickUpBatchStore.list = data
  Taro.navigateTo({
    url: "/pages/pickUp/handleCallSome",
  })
}
//单项转代送
function gotoForwarding(obj) {
  let data = [
    showList.value[obj.index]
  ]
  data[0].orderList = [showList.value[obj.index].orderList[obj.orderIndex]]
  forwardStore.info = data
  Taro.navigateTo({
    url: "/pages/pickUp/forwarding",
  })
}
//单条取货
const pickUpOne = ()=>{
  let billNo = transactionStore.transactionList[0].billNo
  let newArr:any = []
  showList.value.forEach((item,index)=>{
    let checkedBill = item.orderList.filter(s=>{return s.billNo===billNo})
    if(checkedBill.length){
      newArr.push({
        ...item,
        orderList: transactionStore.transactionList
      })
    }
  })
  pickUpBatchStore.list = newArr
  Taro.navigateTo({
    url: '/pages/pickUp/handlePickUp'
  })
}
//批量取货
function pickMore() {
  let newArr:any = []
  showList.value.forEach((item,index)=>{
    let checkedBill = item.orderList.filter(s=>{return s.check})
    if(checkedBill.length>0){
      newArr.push({
        ...item,
        orderList: checkedBill
      })
    }
  })
  pickUpBatchStore.list = newArr
  if (!newArr.length) {
    Taro.showToast({
      icon: "none",
      title: "请先选择订单",
    })
  }
  else if(newArr.length > 1){
    Taro.showToast({
      icon: "none",
      title: "只能选择同网点下的订单",
    })
  }
  else{
    Taro.navigateTo({
      url: '/pages/pickUp/handlePickUp'
    })
  }
}
//批量转代送
function forwardingMore() {
  checkList.value = [];
  let branchCount = 0;
  showList.value.forEach((item,index)=>{
    let checkedBill = item.orderList.filter(s=>{return s.check})
    if(checkedBill.length>0){
      checkList.value = checkList.value.concat(checkedBill)
      branchCount++
    }
  })
  if (!checkList.value.length) {
    Taro.showToast({
      icon: "none",
      title: "请先选择订单",
    })
  }
  else if(branchCount > 1){
    Taro.showToast({
      icon: "none",
      title: "只能选择同网点下的订单",
    })
  }
  else {
    transactionStore.transactionList = checkList.value;
    Taro.navigateTo({
      url: "/pages/pickUp/forwarding",
    });
  }
}
// 初始化列表
async function initList() {
  const res: any = await $post(
    "/selfmachine-system/order/list/miniWaitPickUp",
    {
      startBillingTime: query.value.startBillingTime,
      endBillingTime: query.value.endBillingTime,
      keywords: query.value.keywords,
      customerCode: userStore.userInfo.customerCode,
    }
  );
  if (res.data.code == 200) {
    let arr = res.data.rows || []
    arr = res.data.rows
    arr.forEach((item: any) => {
      item.branchVo.check = false
      item.branchVo.orderList = item.branchVo.orderList || []
      item.branchVo.orderList.forEach((r:any)=>{
        r.check = false
      })
    })
    showList.value = arr.map(item=>{return item.branchVo})
  }
  Taro.hideLoading()

  // 数据全部加载完成
  if (showList.value.length >= res.data.total) {
    loading.value = true;
  }
}

//新版找人代取（批量）
const toCallSome = ()=>{
  let newArr:any = []
  showList.value.forEach((item,index)=>{
    let checkedBill = item.orderList.filter(s=>{return s.check})
    if(checkedBill.length>0){
      newArr.push({
        ...item,
        orderList: checkedBill
      })
    }
  })
  pickUpBatchStore.list = newArr
  if (!newArr.length) {
    Taro.showToast({
      icon: "none",
      title: "请先选择订单",
    })
  }
  else if(newArr.length > 1){
    Taro.showToast({
      icon: "none",
      title: "只能选择同网点下的订单",
    })
  }
  else{
    Taro.navigateTo({
      url: '/pages/pickUp/handleCallSome'
    })
  }
}

useDidShow(()=>{
  Taro.showLoading({
    title: '加载中',
  })
  initList()
})

</script>
<style lang="scss">
.emptyBox {
  height: 176rpx;
}
.big {
  height: 100vh;
}
.search_list {
  // height: 50rpx !important;
  overflow: auto;
  height: calc(100vh - 240rpx);
  padding: 0 24rpx !important;
  box-sizing: border-box;
  background-color: #f5f5f5;

  .item {
    width: 700rpx;
    height: 436rpx;
    background-color: #fff;
    margin: 24rpx;
    border-radius: 10rpx;
    overflow: hidden;
  }
}

.emptyMain {
  margin: 0 auto;
  margin-top: 200rpx !important;
  width: 300rpx;

  .emptyImg {
    height: 300rpx;
    width: 300rpx;
    //background: url("../../assets/images/index/我的订单/wodedingdan_img_quesheng.png") no-repeat;
    //background-size: cover;
  }

  p {
    text-align: center;
    margin-top: 30rpx;
    color: rgb(171, 171, 171);
  }
}

.pickUpfoot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 128rpx;
  width: 100%;
  position: fixed;
  bottom: 0;
  box-sizing: border-box;
  border-top: 2rpx solid rgb(215, 215, 215);
  border-bottom: 2rpx solid rgb(215, 215, 215);
  background: #fff;
  line-height: 128rpx;
  padding: 0 24rpx;

  .pickUpBtnBox {
    display: flex;
    align-items: center;
    .zhaorendaiqu, .zhuandaisong {
      width: 80rpx;
      height: 84rpx;
      margin-right: 32rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      image {
        width: 40rpx;
        height: 40rpx;
        margin-top: 4rpx;
      }
      .img_text {
        font-size: 20rpx;
        margin-top: 8rpx;
        color: #252525;
        font-weight: 500;
        line-height: 20rpx;
      }
    }
  }
}

.divide {
  background-color: transparent;
  margin-top: 24rpx;
  height: 1rpx;
  width: 1rpx;
}
</style>
